package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.SysRoleMapper;
import org.linlinjava.litemall.db.domain.SysRole;
import org.linlinjava.litemall.db.domain.SysRoleExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author zhy
 * @create 2018-09-28 14:17
 **/
@Service
public class LitemallRoleService {
    @Resource
	private SysRoleMapper sysRoleMapper;

    public void deleteById(SysRole sysRole){
	    sysRoleMapper.deleteByPrimaryKey(sysRole.getRid());
    }
    public void addRole(SysRole sysRole){
	    sysRoleMapper.insertSelective(sysRole);
    }
    public void updateRole(SysRole sysRole){
    	sysRoleMapper.updateByPrimaryKeySelective(sysRole);
    }
    public List<SysRole> queryByRname(String rname){
	    SysRoleExample example = new SysRoleExample();
	    example.or().andRnameEqualTo(rname);
	    return sysRoleMapper.selectByExample (example);
    }

	public List<SysRole> queryAll(String rname,Integer page, Integer size, String sort, String order){
		SysRoleExample example = new SysRoleExample();
		SysRoleExample.Criteria criteria=example.createCriteria();
		if(!StringUtils.isEmpty(rname)){
			criteria.andRnameLike("%" + rname + "%");
		}
		PageHelper.startPage(page, size);
		return sysRoleMapper.selectByExample (example);
	}

	public List<SysRole> queryAll2(){
		SysRoleExample example = new SysRoleExample();
		return sysRoleMapper.selectByExample (example);
	}

	public List<Map> getUserRole(String uesrId) {
		return sysRoleMapper.getUserRole(uesrId);
	}

	public int countAll(String rname,Integer page, Integer size, String sort, String order){
		SysRoleExample example = new SysRoleExample();
		SysRoleExample.Criteria criteria=example.createCriteria();
		if(!StringUtils.isEmpty(rname)){
			criteria.andRnameLike("%" + rname + "%");
		}
		return (int)sysRoleMapper.countByExample(example);
	}

	public SysRole selectById(Integer rids){
    	return sysRoleMapper.selectByPrimaryKey(rids);
	}
}
